﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;

namespace ArduinoSketch.Core
{
    public partial class HardwareTargetDialog : Form
    {
        public enum ActionType : int
        {
            NEW = 0,
            UPDATE
        }

        private static HardwareProfile _profile;

        private void ShowMessageDialog(string msg)
        {
            PSTaskDialog.cTaskDialog.MessageBox(
                            "Warning",
                            "Hardware profile",
                            msg,
                            eTaskDialogButtons.OK,
                            eSysIcons.Warning);
        }

        public HardwareProfile Profile
        {
            get { return _profile; }
            set 
            { 
                _profile = value;
                this.txtName.Text = _profile.Name;
                this.txtMcu.Text = _profile.Mcu;
                this.txtFcpu.Text = _profile.Fcpu;
                this.txtCore.Text = _profile.Core;
                this.txtProtocol.Text = _profile.UploadPotocal;
                this.txtMaximumSize.Text = _profile.UploadMaximum;
                this.txtSpeed.Text = _profile.UploadSpeed;
                this.txtLowFuses.Text = _profile.BootloaderLowFuses;
                this.txtHighFuses.Text = _profile.BootloaderHighFuses;
                this.txtExtenedFuses.Text = _profile.BootloaderExtenedFuses;
                this.txtBootloaderFile.Text = _profile.BootloaderFile;
                this.txtLockBit.Text = _profile.BootloaderLockBit;
                this.txtUnloacBit.Text = _profile.BootloaderUnlockBit;
            }
        }

        public HardwareTargetDialog()
        {
            InitializeComponent();
        }

        private bool ValidateData()
        {

            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                ShowMessageDialog("Profile Name is not empty"); this.txtName.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtMcu.Text))
            {
                ShowMessageDialog("Mcu is not empty"); this.txtMcu.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtFcpu.Text))
            {
                ShowMessageDialog("Fcpu is not empty"); this.txtFcpu.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtCore.Text))
            {
                ShowMessageDialog("Build core is not empty"); this.txtCore.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtProtocol.Text))
            {
                ShowMessageDialog("Upload Protocolis not empty"); this.txtProtocol.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtMaximumSize.Text))
            {
                ShowMessageDialog("Upload Maximum Sizeis not empty"); this.txtMaximumSize.Focus(); return false;
            }

            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                ShowMessageDialog("Upload Speed is not empty"); this.txtName.Focus(); return false;
            }
   
            return true;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                this.Profile.Name = this.txtName.Text;
                this.Profile.Mcu = this.txtMcu.Text;
                this.Profile.Fcpu = this.txtFcpu.Text;
                this.Profile.Core = this.txtCore.Text;
                this.Profile.UploadPotocal = this.txtProtocol.Text;
                this.Profile.UploadMaximum = this.txtMaximumSize.Text;
                this.Profile.UploadSpeed = this.txtSpeed.Text;
                this.Profile.BootloaderLowFuses = this.txtLowFuses.Text;
                this.Profile.BootloaderHighFuses = this.txtHighFuses.Text;
                this.Profile.BootloaderExtenedFuses = this.txtExtenedFuses.Text;
                this.Profile.BootloaderFile = this.txtBootloaderFile.Text;
                this.Profile.BootloaderLockBit = this.txtLockBit.Text;
                this.Profile.BootloaderUnlockBit = this.txtUnloacBit.Text;

                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog.Filter = "Intel hex file|*.hex|All file|*.*";
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo _info = new FileInfo(this.openFileDialog.FileName);
                this.txtBootloaderFile.Text = _info.Name;
                
            }
        }

        public static HardwareProfile GetHardwareProfile()
        {
            return _profile;
        }

        public static DialogResult ShowNewDialog()
        {
            HardwareTargetDialog _dialog = new HardwareTargetDialog();
            _dialog.Profile = new HardwareProfile();

            return _dialog.ShowDialog();
        }

        public static DialogResult ShowUpdateDialog(HardwareProfile profile)
        {
            HardwareTargetDialog _dialog = new HardwareTargetDialog();
            _dialog.Profile = profile;

            return _dialog.ShowDialog();
        }
    }
}
